/*
 * @Descripttion: 无效验证、权限验证、登录验证
 * @version:
 * @Author: 周志国
 * @Date: 2021-06-21 13:45:35
 * @LastEditors: 周志国
 * @LastEditTime: 2021-12-27 11:50:10
 * @changeLog:
 * @FilePath: /anov-next/src/assets/js/app/indexhandler/validateHandler.js
 */
import { licenseServer, permissionServer, runtimeServer } from 'anov-core'
import { modal } from '@/assets/js/app/message'
import loading from './loading.js'
import log from '../utils/sys-utils/log'
import appStore from '@/store'
import { getCookie } from '../../../../utils/cookie'

const event = {
  //登陆成功
  loginSuccess: 'loginSuccess',
  //登陆失败
  loginError: 'loginError',
  //license 验证不通过
  licenseNoPass: 'licenseNoPass',
  //license 验证通过
  licensePass: 'licensePass',
  //不需要登陆
  noNeedLogin: 'noNeedLogin'
}
//跳过项目信息验证
// licenseServer._validateProject = () => {
//   return true
// }
permissionServer.addEventListener('loginSuccess', (e) => {
  log('loginValidate success')
  validateHandler.$emit(validateHandler.event.loginSuccess)
  loading.state == 'hide' && loading.show()
})
permissionServer.addEventListener('loginError', (e) => {
  log('loginValidate error')
  validateHandler.$emit(validateHandler.event.loginError)
})

const validateHandler = {
  ...runtimeServer.getEventBus(),
  event,
  licenseValidate(config = {}) {
    let expireDayFlag = sessionStorage.getItem('expireDayFlag')
    if (!expireDayFlag) {
      licenseServer.addEventListener('expireDay', (e) => {
        modal.open({
          type: e.result.type,
          componentsName: 'expireDay'
        })
        sessionStorage.setItem('expireDayFlag', true)
      })
    }
    licenseServer.addEventListener('noPass', (e) => {
      //根据loading progress判断显示哪个无效弹窗
      // if(appStore.state.loadingProgress === 100){
      if (loading.state == 'hide') {
        modal.open({
          type: 'error',
          componentsName: 'validPeriod',
          content: '无效的许可证',
          style: {
            width: '800px',
            height: 'auto'
          }
        })
      } else {
        this.initValidError({ ...e.result, chip: appStore.state.env.chip })
      }
      log('license noPass')
      this.$emit(this.event.licenseNoPass)
    })
    licenseServer.addEventListener('pass', (e) => {
      log('license Pass')
      this.$emit(this.event.licensePass)
    })
    licenseServer.init(config)
  },
  async loginValidate(config) {
    let login = config.permission.login
    let tipStr = 'no login'
    if (login && getCookie('token')) {
      const res = await appStore.dispatch('login/getUserInfo')
      if (res && res.user) {
        appStore.commit('login/setToken', getCookie('token'))
        appStore.commit('login/setUser', res.user)
        appStore.commit('setTokenId', getCookie('token'))
        appStore.commit('permission/setIsLogin', true)
        tipStr = 'login by token'
        login = false
      }
    }
    if (login === false) {
      log(tipStr)
      this.$emit(this.event.noNeedLogin)
      return
    }
    log('need login')
    //权限
    permissionServer.init(config)
    permissionServer.loginId = config.permission.defaultId
    permissionServer.openLogin()
    loading.state == 'show' && loading.hide()
  },
  initValidError(res) {
    loading.state == 'show' && loading.hide()
    modal.open({
      type: 'error',
      componentsName: 'validError',
      content: res ? res : '无效许可证'
    })
  }
}
export default validateHandler
